import { plainToInstance } from "class-transformer";
import { UserEntity } from "../entities/UserEntity";
import ResultHelper from "../routes/utils/ResultHelper";
import UserDao from "../dao/UserDao";
import MD5 from "md5";
import * as _ from "lodash";
import md5 from "md5";
import EmailCodeService from "./EmailCodeService";
import { ResponseCode } from "../routes/utils/ResponseHelper";

export default class UserService {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  static async register(body: any) {
    const User = plainToInstance(UserEntity, body);
    const errs = await User.validateThie();

    if (errs.length > 0) {
      return ResultHelper.error(errs);
    }

    try {
      const verifyResult = await EmailCodeService.verify(User.email, body.code);
      if (verifyResult.code !== ResponseCode.PASS) {
        return verifyResult;
      }

      User.password = MD5(User.password);
      const result = await UserDao.register(User);

      return ResultHelper.success(
        _.omit(result, "password", "createdAt", "updatedAt")
      );
    } catch ({ message }) {
      return ResultHelper.error(message);
    }
  }

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  static async login(body: any) {
    if (!body.email) {
      return ResultHelper.error("请输入邮箱");
    }

    if (!body.password) {
      return ResultHelper.error("请输入密码");
    }

    const User = plainToInstance(UserEntity, body);
    const errs = await User.validateThie(true);
    if (errs.length > 0) {
      return ResultHelper.error(errs);
    }

    try {
      User.password = md5(User.password);
      const result = await UserDao.login(User);
      if (result === null) {
        return ResultHelper.error("账号或密码错误");
      }
      return ResultHelper.success(result);
    } catch ({ message }) {
      return ResultHelper.error(message);
    }
  }

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  static async whoami(body: any) {
    const User = plainToInstance(UserEntity, body);
    const errs = await User.validateThie(true);
    if (errs.length != 0) {
      return ResultHelper.error(errs);
    }

    try {
      const result = await UserDao.whoami(User);
      if (result === null) {
        return ResultHelper.error("用户不存在");
      }

      return ResultHelper.success(result);
    } catch ({ message }) {
      return ResultHelper.error(message);
    }
  }

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  static async modifyPassword(body: any) {
    const User = plainToInstance(UserEntity, body);
    const errs = await User.validateThie(true);
    if (errs.length != 0) {
      return ResultHelper.error(errs);
    }

    try {
      const verifyResult = await EmailCodeService.verify(User.email, body.code);
      if (verifyResult.code !== ResponseCode.PASS) {
        return verifyResult;
      }

      User.password = md5(User.password);
      const result = await UserDao.modifyPassword(User);
      return result
        ? ResultHelper.success({}, "修改成功")
        : ResultHelper.error("修改失败");
    } catch ({ message }) {
      return ResultHelper.error(message);
    }
  }
}
